<?php

/**
 * Catalog class to manage general catalog functions
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 *
 * Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class Catalog
{
	// SQL statements
	var $_categorySql = 'select count(products_id) as product_count, c.categories_id, cd.categories_name, c.parent_id from %s c left join %s p2c on c.categories_id = p2c.categories_id, %s cd where c.parent_id = "%s" and c.categories_id = cd.categories_id and cd.language_id = "%s" group by categories_id order by c.sort_order, cd.categories_name';
	
	var $_manufacturerSql = 'select count(products_id) as product_count, m.manufacturers_id, m.manufacturers_name, mi.manufacturers_url from %s m, %s mi, %s p where m.manufacturers_id = mi.manufacturers_id and m.manufacturers_id = p.manufacturers_id and mi.languages_id = "%s" group by m.manufacturers_id order by m.manufacturers_name';
	
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param string $formVar The name of the submit button in your form
	 */
	
	function Catalog()
	{
	}
	
	/**
	 * Get the child categories for a given category id
	 *
	 * @access public
	 * @param integer $id Default: -1 (root categories)
	 * @return array An array of category objects
	 */
	
	function getCategories($id = -1)
	{
		global $lng;
		
		if (!is_numeric($id) || $id < 1)
		{
			$id = 'NULL';
		}
		
		$categories = array();
		$categoriesQuery = tep_db_query(sprintf($this->_categorySql, TABLE_CATEGORIES, TABLE_PRODUCTS_TO_CATEGORIES, TABLE_CATEGORIES_DESCRIPTION, $id, $lng->getLanguageId()));
		
		while ($row = tep_db_fetch_array($categoriesQuery))
		{
			$categories[] = new Category($row);
		}
		
		return $categories;
	}
	
	/**
	 * Get the child categories for a given category id
	 *
	 * @access public
	 * @param integer $id Default: -1 (root categories)
	 * @return array An array of category objects
	 */
	
	function getManufacturers()
	{
		global $lng;
		
		$manufacturers = array();
		$manufacturersQuery = tep_db_query(sprintf($this->_manufacturerSql, TABLE_MANUFACTURERS, TABLE_MANUFACTURERS_INFO, TABLE_PRODUCTS, $lng->getLanguageId()));
		
		while ($row = tep_db_fetch_array($manufacturersQuery))
		{
			$manufacturers[] = new Manufacturer($row);
		}
		
		return $manufacturers;
	}
	
	/**
	 * Returns an instance of Catalog of the type requested
	 *
	 * @access public
	 * @param string $identifier The catalog module to use
	 */
	
	function getModule($identifier)
	{
		if (!ereg('^([A-Za-z0-9]+)\.([A-Za-z0-9]+)$', $identifier))
		{
			return null;
		}
		
		$classes = get_declared_classes();
		$classesSize = sizeof($classes);
		
		if (!is_dir(DIR_FS_MODULES.$identifier))
		{
			return null;
		}
		
		$classFile = DIR_FS_MODULES.$identifier.'/module.php';
		
		if (!file_exists($classFile))
		{
			return null;
		}
		
		include_once($classFile);
		$classes = get_declared_classes();
		$newClassesSize = sizeof($classes);
		
		if ($newClassesSize <= $classesSize)
		{
			return null;
		}
		
		$indexOffset = $newClassesSize - $classesSize;
		$requestedClass = null;
		
		while ($indexOffset > 0)
		{
			$newClass = $classes[$newClassesSize - $indexOffset--];
			$object = new $newClass();
			
			if (is_subclass_of($object, 'CatalogModule') && $object->getIdentifier() == $identifier)
			{
				$requestedClass = $object;
			}
		}
		
		return $requestedClass;
	}
}

/**
 * Category class
 */

class Category
{
	var $_id;
	var $_name;
	var $_parentId;
	var $_productCount;
	
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param integer|array $init Initializer: either category id or array
	 */
	
	function Category($init)
	{
		if (is_numeric($init))
		{
		}
		
		elseif (is_array($init))
		{
			$this->_id = $init['categories_id'];
			$this->_name = $init['categories_name'];
			$this->_parentId = $init['parent_id'];
			$this->_productCount = $init['product_count'];
		}
	}
	
	function getId()
	{
		return $this->_id;
	}
	
	function getName()
	{
		return $this->_name;
	}
	
	function getParentId()
	{
		return $this->_parentId;
	}
	
	function getProductCount()
	{
		return $this->_productCount;
	}
}

/**
 * Manufacturers class
 */

class Manufacturer
{
	var $_id;
	var $_name;
	var $_productCount;
	var $_url;
	
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param integer|array $init Initializer: either manufacturer id or array
	 */
	
	function Manufacturer($init)
	{
		if (is_numeric($init))
		{
		}
		
		elseif (is_array($init))
		{
			$this->_id = $init['manufacturers_id'];
			$this->_name = $init['manufacturers_name'];
			$this->_productCount = $init['product_count'];
			$this->_url = $init['manufacturers_url'];
		}
	}
	
	function getId()
	{
		return $this->_id;
	}
	
	function getName()
	{
		return $this->_name;
	}
	
	function getProductCount()
	{
		return $this->_productCount;
	}
	
	function getUrl()
	{
		return $this->_url;
	}
}

?>
